package com.moneywelcome.investassembly.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.moneywelcome.exceptions.MwcArgsException;
import com.moneywelcome.goldfacade.GoldQueryApi;
import com.moneywelcome.investfacade.InvestInfoApi;
import com.moneywelcome.investfacade.dto.*;
import com.moneywelcome.orderfacade.FinancialApi;
import com.moneywelcome.results.Result;
import com.moneywelcome.results.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "用户投资信息服务")
public class InvestInfoApiImpl implements InvestInfoApi {
    @Autowired
    private GoldQueryApi gqApi;
    @Autowired
    private FinancialApi financialApi;
    @ApiOperation("黄金产品信息")
    @Override
    @ApiImplicitParams(
            @ApiImplicitParam(name = "goldId",value = "黄金产品id/53,55,56",required = true)
    )
    public Result<GoldInfoDto> getProductInfo(Integer goldId) throws IOException {
        if (BeanUtil.isEmpty(gqApi.queryById(goldId).getData())) throw new MwcArgsException("黄金产品不存在", StatusCode.Error);
        return Result.success(BeanUtil.copyProperties(gqApi.queryById(goldId).getData(),GoldInfoDto.class)) ;
    }
    @ApiOperation("获取黄金产品实时信息")
    @Override
    @ApiImplicitParams(
            @ApiImplicitParam(name = "goldId",value = "黄金产品id/53,55,56",required = true)
    )
    public Result getCurrentInfo(Integer goldId) throws IOException, ClassNotFoundException {
        if (BeanUtil.isEmpty(gqApi.queryById(goldId).getData())) throw new MwcArgsException("黄金产品不存在", StatusCode.Error);

        return Result.success(BeanUtil.copyProperties(gqApi.getCurrentInfo(goldId.toString()).getData(),ProductInfoDto.class));
    }
    @ApiOperation("查询所有产品")
    @Override
    public Result<List<GoldInfoDto>> getProductInfos() {
        return Result.success(BeanUtil.copyToList(gqApi.queryALL().getData(),GoldInfoDto.class));
    }
    @ApiOperation("查询购买规则")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "goldId",value = "黄金产品id/51-60",required = true)
    )
    @Override
    public Result<SubscribeRuleDto> getSubRule(Integer goldId) {
        if (BeanUtil.isEmpty(gqApi.queryById(goldId).getData())) throw new MwcArgsException("黄金产品不存在", StatusCode.Error);
        return Result.success(BeanUtil.copyProperties( gqApi.querySubRule(goldId).getData(),SubscribeRuleDto.class));
    }
    @ApiOperation("查询赎回规则")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "goldId",value = "黄金产品id/51-60",required = true)
    )
    @Override
    public Result<RedeemRuleDto> getRedeemRule(Integer goldId) {
        if (BeanUtil.isEmpty(gqApi.queryById(goldId).getData())) throw new MwcArgsException("黄金产品不存在", StatusCode.Error);
        return Result.success(BeanUtil.copyProperties( gqApi.queryRedeemRule(goldId).getData(),RedeemRuleDto.class));
    }
    @ApiOperation("查询产品协议")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "goldId",value = "黄金产品id/51-60",required = true)
    )
    @Override
    public Result<GoldAgreemenDto> getAgreement(Integer goldId) {
        if (BeanUtil.isEmpty(gqApi.queryById(goldId).getData())) throw new MwcArgsException("黄金产品不存在", StatusCode.Error);
        return Result.success(BeanUtil.copyProperties(gqApi.queryAggre(goldId).getData() ,GoldAgreemenDto.class));
    }

}
